const login = async () => {
    return await new Promise((resolve, reject) => {
        uni.login({
            provider: "weixin",
            success: (res) => {
                if (res.code) {
                    resolve(res);
                } else {
                    reject(res);
                }
            },
            fail: (err) => {
                console.log("uni.login 接口调用失败，将无法正常使用开放接口等服务", err);
                reject(err);
            },
        });
    });
};

const checkSession = () => {
    return new Promise(function (resolve, reject) {
        uni.checkSession({
            success: (res) => {
                resolve(res);
            },
            fail: (err) => {
                reject(err);
            },
        });
    });
};

/**
 * 获取用户信息
 */
const getUserProfile = async () => {
    return await new Promise((resolve, reject) => {
        uni.getUserProfile({
            desc: "完善个人信息",
            lang: "zh_CN",
            success: (res) => {
                resolve(res.userInfo);
            },
            fail: (err) => {
                reject(err);
            },
        });
    });
};

const getUserInfo = async () => {
    return await new Promise((resolve, reject) => {
        uni.getUserInfo({
            provider: "weixin",
            success: (res) => {
                resolve(res);
            },
            fail: (err) => {
                reject(err);
            },
        });
    });
};

export {login, checkSession, getUserProfile, getUserInfo};
